package com.zlzxm.shoesmarket.view.activity;

import android.content.Context;
import android.content.Intent;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.zlzxm.shoesmarket.R;
import com.zlzxm.shoesmarket.util.ZViewHelp;
import com.zlzxm.shoesmarket.view.adapter.ADManageInfo;
import com.zlzxm.shoesmarket.view.markview.MarkViewPrice;
import com.zlzxm.shoesmarket.view.widget.simpleead.SimpleHead;
import com.zlzxm.shoesmarket.viewmodel.VMManageDesc;
import com.zlzxm.shoesmarket.viewmodel.entity.EntityIn;

import java.util.ArrayList;
import java.util.List;

/**
 * CREATE BY zlzxm ON 4/22/21
 * <br>
 * 经营概况
 *
 */
public class ACManageDesc extends BaseActivity{

    public static void start(Context context) {
        Intent starter = new Intent(context, ACManageDesc.class);
        context.startActivity(starter);
    }

    private VMManageDesc vmManageDesc;

    private RecyclerView rvManageInfo;

    private ADManageInfo adManageInfo = new ADManageInfo();


    private LineChart lcIn;

    @Override
    protected void initView() {
        super.initView();
        ZViewHelp.setStatusBarTransparent(getWindow(),true);
        setContentView(R.layout.ac_manage_desc);
        SimpleHead simpleHead = ZViewHelp.findById(this,R.id.head);
        simpleHead.setTitle("经营概况");
        simpleHead.setOnItemclicklistener(this::finish);
        rvManageInfo =  ZViewHelp.findById(this,R.id.rvManageInfo);
        rvManageInfo.setLayoutManager(new LinearLayoutManager(this));
        rvManageInfo.setAdapter(adManageInfo);

        lcIn = ZViewHelp.findById(this,R.id.lcIn);

        initInLineChart();
    }

    @Override
    protected void initViewModule() {
        super.initViewModule();
        vmManageDesc = new ViewModelProvider(this)
                .get(VMManageDesc.class);

        vmManageDesc.getLiveManage().observe(this,adManageInfo::setList);

        vmManageDesc.getLiveIn().observe(this,this::initInChartData);
    }

    @Override
    protected void initData() {
        super.initData();
        vmManageDesc.requestInfo();
        vmManageDesc.getInData();
    }


    private void initInLineChart() {


        lcIn.setDragEnabled(false);
        //设置推动
        lcIn.setScaleEnabled(false);

        lcIn.setMarker(new MarkViewPrice(this));

        YAxis leftAxis = lcIn.getAxisLeft();
        YAxis rightAxis = lcIn.getAxisRight();


        leftAxis.setTextColor(ContextCompat.getColor(this,R.color.colorA7A7A7));
        leftAxis.setTextSize(12);

        leftAxis.setAxisMinimum(0);
        rightAxis.setAxisMinimum(0);

        leftAxis.setDrawLabels(true);
        rightAxis.setDrawLabels(false);

        leftAxis.setDrawAxisLine(false);
        rightAxis.setDrawAxisLine(false);

        leftAxis.setAxisMaximum(100);
        rightAxis.setAxisMaximum(100);

        leftAxis.setLabelCount(6,true);
        rightAxis.setLabelCount(6,true);


        leftAxis.setGridColor(ContextCompat.getColor(this,
                R.color.colorA8A8A8));
        rightAxis.setGridColor(ContextCompat.getColor(this,
                R.color.colorA8A8A8));

        lcIn.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);

        lcIn.getLegend().setEnabled(true);

        lcIn.getXAxis().setTextColor(ContextCompat.getColor(this,
                R.color.colorA7A7A7));

        lcIn.getXAxis().setTextSize(12);

        Description description = new Description();
        description.setText("");
        lcIn.setDescription(description);

        lcIn.getXAxis().setDrawGridLines(false);

        Legend legend =  lcIn.getLegend();

        legend.setForm(Legend.LegendForm.SQUARE);

        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
        legend.setFormSize(10);
        legend.setXEntrySpace(20);
        legend.setTextSize(13);
        legend.setTextColor(ContextCompat.getColor(this,R.color.color999999));
        lcIn.setExtraBottomOffset(20);
        lcIn.setExtraTopOffset(20);




        LineDataSet lineDataSetOut = new LineDataSet(new ArrayList<>(), "货币组合");
        LineDataSet lineDataSetIn = new LineDataSet(new ArrayList<>(), "市场平均");

        lineDataSetOut.setDrawHorizontalHighlightIndicator(false);
        lineDataSetOut.setDrawVerticalHighlightIndicator(false);

        lineDataSetOut.setColor(ContextCompat
                .getColor(this,R.color.colorE44444));

        lineDataSetOut.setLineWidth(2f);

        lineDataSetOut.setDrawCircles(false);


        lineDataSetOut.setDrawValues(false);

        lineDataSetOut.setMode(LineDataSet.Mode.LINEAR);

        lineDataSetIn.setDrawHorizontalHighlightIndicator(false);
        lineDataSetIn.setDrawVerticalHighlightIndicator(false);

        lineDataSetIn.setColor(ContextCompat
                .getColor(this,R.color.color448DE4));

        lineDataSetIn.setLineWidth(2f);

        lineDataSetIn.setDrawCircles(false);

        lineDataSetIn.setDrawValues(false);

        lineDataSetIn.setMode(LineDataSet.Mode.LINEAR);


        List<ILineDataSet> dataSets = new ArrayList<>();

        dataSets.add(lineDataSetOut);
        dataSets.add(lineDataSetIn);

        LineData lineData = new LineData(dataSets);

        lcIn.setData(lineData);


    }


    private void initInChartData(EntityIn entityInOut){

        List<Entry> dataIn  = new ArrayList<>();

        for (int i = 0; i < entityInOut.getListCost().size(); i++) {

            dataIn.add(new Entry(i,entityInOut.getListCost().get(i).getValue()));

        }


        List<Entry> dataOut  = new ArrayList<>();

        for (int i = 0; i < entityInOut.getListAvg().size(); i++) {

            dataOut.add(new Entry(i,entityInOut.getListAvg().get(i).getValue()));

        }


        lcIn.getXAxis().setValueFormatter(new ValueFormatter(){

            @Override
            public String getAxisLabel(float value, AxisBase axis) {


                return entityInOut.getListAvg().get((int) value).getKey();
            }
        });


        LineData lineData = lcIn.getLineData();

        LineDataSet lineDataSetOut = (LineDataSet) lineData.getDataSetByIndex(0);

        lineDataSetOut.setValues(dataOut);

        lineDataSetOut.notifyDataSetChanged();


        LineDataSet lineDataSetIn = (LineDataSet) lineData.getDataSetByIndex(1);

        lineDataSetIn.setValues(dataIn);

        lineDataSetIn.notifyDataSetChanged();

        lineData.notifyDataChanged();

        lcIn.notifyDataSetChanged();

        lcIn.invalidate();


    }

}
